<?php
!defined( 'IN_HDWIKI' ) && exit( 'Access Denied' );

define( 'MB_RETURN_FORMAT', 'json' );
define( 'MB_API_HOST', 'open.t.qq.com' );
require_once( HDWIKI_ROOT . '/plugins/socialconnect/lib/qq/oauth.php' );
require_once( HDWIKI_ROOT . '/plugins/socialconnect/lib/qq/opent.php' );

class control extends base
{
	function control( & $get, & $post )
	{
		$this->base( $get, $post );
		$this->load( 'plugin' );
		$this->load( 'user' );
		$this->loadplugin( 'socialconnect' );

		if ( '0' == $this->plugin['socialconnect']['vars']['qq_connect_on'] ) {
			$this->header();
		}

		$this->setting['qq_akey'] = $this->plugin['socialconnect']['vars']['qq_app_key'];
		$this->setting['qq_skey'] = $this->plugin['socialconnect']['vars']['qq_secret_key'];
	}

	function dooauth()
	{
		if ( $this->get[2] != 'bind' && $_ENV['user']->is_login() ) {
			$this->header();
		}

		session_start();
		if ( isset( $_SESSION['qq_oauth_keys'] ) ) {
			unset( $_SESSION['qq_oauth_keys'] );
		}

		$o = new MBOpenTOAuth( $this->setting['qq_akey'], $this->setting['qq_skey'] );
		$callback = $this->setting['site_url'].'/index.php?user-login.php&callback_from=qq'.($this->get[2]=='bind'?'&oauth_action=bind':'');
		$keys = $o->getRequestToken( $callback );
		$aurl = $o->getAuthorizeURL( $keys['oauth_token'], false, '' );
		$_SESSION['qq_oauth_keys'] = $keys;

		header( "Location:$aurl" );
	}

	function dounbind()
	{
		if ( !$_ENV["user"]->is_login() ) {
			$this->header( 'user-login' );
		}
		$this->loadplugin( 'qq', 'socialconnect' );
		$_ENV['qq']->unbind( $this->user['uid'] );
		$this->header( 'plugin-socialconnect' );
	}
}
?>
